package com.webcash.CNBoard.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.webcash.CNBoard.Model.CalendarModel;
import com.webcash.CNBoard.Model.Week;

public class CalendarUtil {

	Calendar cal;
	int nYear;
	int nMonth;
	int nDate;
	int nNumday;
	String[] dayName;
	int col;
	int fNumday;
	int lastDay;

	public CalendarUtil() {
		cal = Calendar.getInstance();
		nYear = cal.get(cal.YEAR);
		nMonth = cal.get(cal.MONTH);
		nDate = cal.get(cal.DATE);
		nNumday = cal.get(cal.DAY_OF_WEEK);
		dayName = new String[] { "일", "월", "화", "수", "목", "금", "토" };
		col = 0;
		cal.set(nYear, nMonth, 1);
		fNumday = cal.get(cal.DAY_OF_WEEK);
		lastDay = cal.getActualMaximum(cal.DATE);
		
	}

	public CalendarUtil(int year, int month) {

		cal = Calendar.getInstance();
		
		cal.set(year, month, 1);
		
		nYear = cal.get(cal.YEAR);
		nMonth = cal.get(cal.MONTH);
		nDate = cal.get(cal.DATE);
		nNumday = cal.get(cal.DAY_OF_WEEK);
		dayName = new String[] { "일", "월", "화", "수", "목", "금", "토" };
		col = 0;
		cal.set(nYear, nMonth, 1);
		fNumday = cal.get(cal.DAY_OF_WEEK);
		lastDay = cal.getActualMaximum(cal.DATE);
		
		Calendar c = Calendar.getInstance();
		if(nYear == c.get(cal.YEAR) && nMonth == c.get(cal.MONTH)){
			nDate = c.get(c.DATE);
		}else{
			nDate = 0;
		}

	}
	
	public CalendarModel week(CalendarModel calModel, int year, int month, int day){
		CalendarModel cal = calModel;
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month + 1, day); //현재 날짜 지정
		
		Calendar startWeek = (Calendar)calendar.clone();
		int w = startWeek.get(Calendar.DAY_OF_WEEK);
		startWeek.add(Calendar.DATE, -(w-1)); // 주 첫번째 날자 구하기
		
		
		Calendar endWeek = (Calendar)calendar.clone();
		int w2 = endWeek.get(Calendar.DAY_OF_WEEK);
		endWeek.add(Calendar.DATE, 7-w2); //주 마지막 날짜 구하기
		
		Week lastDayOfWeek = new Week();
		
		lastDayOfWeek.setYear(endWeek.get(Calendar.YEAR));
		lastDayOfWeek.setMonth(endWeek.get(Calendar.MONTH));
		lastDayOfWeek.setDay(endWeek.get(Calendar.DATE));
		lastDayOfWeek.setWeek_of_month(endWeek.get(Calendar.WEEK_OF_MONTH));
		
		cal.setEndWeekYear(endWeek.get(Calendar.YEAR));
		cal.setEndWeekMonth(endWeek.get(Calendar.MONTH));
		cal.setEndWeekDay(endWeek.get(Calendar.DATE));
		
		

		Week starDayOfWeek = new Week();
		
		starDayOfWeek.setYear(startWeek.get(Calendar.YEAR));
		starDayOfWeek.setMonth(startWeek.get(Calendar.MONTH));
		starDayOfWeek.setDay(startWeek.get(Calendar.DATE));
		starDayOfWeek.setWeek_of_month(startWeek.get(Calendar.WEEK_OF_MONTH));
		
		cal.setStartWeekYear(startWeek.get(Calendar.YEAR));
		cal.setStartWeekMonth(startWeek.get(Calendar.MONTH));
		cal.setStartWeekDay(startWeek.get(Calendar.DATE));
		
		List<Week> weekList = new ArrayList<Week>();
		
		weekList.add(starDayOfWeek);
		
		
		
		while(startWeek.get(Calendar.DATE) != lastDayOfWeek.getDay()){
			
			startWeek.set(Calendar.DATE, startWeek.get(Calendar.DATE) + 1);
			Week newWeek = new Week();
			newWeek.setYear(startWeek.get(Calendar.YEAR));
			newWeek.setMonth(startWeek.get(Calendar.MONTH));
			newWeek.setDay(startWeek.get(Calendar.DATE));
			newWeek.setWeek_of_month(startWeek.get(Calendar.WEEK_OF_MONTH));
			weekList.add(newWeek);
		}
		
		
		cal.setWeekList(weekList);
		
		
		return cal;
	}

	public CalendarModel calendar() {
		CalendarModel cal = new CalendarModel();
		cal.setCurrentYear(nYear);
		cal.setCurrentMonth(nMonth);
		cal.setCurrentDay(nDate);
		cal.setCurrentYoil(nNumday);
		cal.setDayNames(dayName);
		cal.setCol(col);
		cal.setFirstDay(fNumday);
		cal.setLastDay(lastDay);
		return cal;
	}

	public CalendarModel weekUpAndDown(CalendarModel calModel, int year, int month,
			int day, boolean upAndDown) {
		CalendarModel cal = calModel;
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month + 1, day); //현재 날짜 지정
		
		if(upAndDown){
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 7);
		}else{
			calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
			
		}
		
		Calendar startWeek = (Calendar)calendar.clone();
		int w = startWeek.get(Calendar.DAY_OF_WEEK);
		startWeek.add(Calendar.DATE, -(w-1)); // 주 첫번째 날자 구하기
		
		
		Calendar endWeek = (Calendar)calendar.clone();
		int w2 = endWeek.get(Calendar.DAY_OF_WEEK);
		endWeek.add(Calendar.DATE, 7-w2); //주 마지막 날짜 구하기
		
		Week lastDayOfWeek = new Week();
		
		lastDayOfWeek.setYear(endWeek.get(Calendar.YEAR));
		lastDayOfWeek.setMonth(endWeek.get(Calendar.MONTH));
		lastDayOfWeek.setDay(endWeek.get(Calendar.DATE));
		lastDayOfWeek.setWeek_of_month(endWeek.get(Calendar.WEEK_OF_MONTH));
		
		cal.setEndWeekYear(endWeek.get(Calendar.YEAR));
		cal.setEndWeekMonth(endWeek.get(Calendar.MONTH));
		cal.setEndWeekDay(endWeek.get(Calendar.DATE));
		
		

		Week starDayOfWeek = new Week();
		
		starDayOfWeek.setYear(startWeek.get(Calendar.YEAR));
		starDayOfWeek.setMonth(startWeek.get(Calendar.MONTH));
		starDayOfWeek.setDay(startWeek.get(Calendar.DATE));
		starDayOfWeek.setWeek_of_month(startWeek.get(Calendar.WEEK_OF_MONTH));
		
		cal.setStartWeekYear(startWeek.get(Calendar.YEAR));
		cal.setStartWeekMonth(startWeek.get(Calendar.MONTH));
		cal.setStartWeekDay(startWeek.get(Calendar.DATE));
		
		List<Week> weekList = new ArrayList<Week>();
		
		weekList.add(starDayOfWeek);
		
		
		
		while(startWeek.get(Calendar.DATE) != lastDayOfWeek.getDay()){
			
			startWeek.set(Calendar.DATE, startWeek.get(Calendar.DATE) + 1);
			Week newWeek = new Week();
			newWeek.setYear(startWeek.get(Calendar.YEAR));
			newWeek.setMonth(startWeek.get(Calendar.MONTH));
			newWeek.setDay(startWeek.get(Calendar.DATE));
			newWeek.setWeek_of_month(startWeek.get(Calendar.WEEK_OF_MONTH));
			weekList.add(newWeek);
		}
		
		
		cal.setWeekList(weekList);
		
		
		return cal;
	}
	
}
